﻿<?PHP
/*
    Registration/Login script from HTML Form Guide
    V1.0

    This program is free software published under the
    terms of the GNU Lesser General Public License.
    http://www.gnu.org/copyleft/lesser.html
    

This program is distributed in the hope that it will
be useful - WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

For updates, please visit:
http://www.html-form-guide.com/php-form/php-registration-form.html
http://www.html-form-guide.com/php-form/php-login-form.html

*/
require_once("class.phpmailer.php");
require_once("class.smtp.php");
require_once("formvalidator.php");
require_once("debug.php");

class FGMembersite
{
    var $from_address;
    
    var $username;
    var $pwd;
    var $database;
    var $connection;
    var $rand_key;
    
    var $error_message;
    var $db_error_message;
    
    var $table_users = "users";
    var $table_groups = "groups";
	var $table_lineups = "lineups";

    //-----Initialization -------
    function FGMembersite()
    {
        $this->sitename = 'YourWebsiteName.com';
        $this->rand_key = '0iQx5oBk66oVZep';
    }
    function InitSESSION()
    {
		$this->InsertArrayItem($_SESSION, 'vn', 0);
		$this->InsertArrayItem($_SESSION, 'device', '');
		$this->InsertArrayItem($_SESSION, 'main-div-left', 0);
		$this->InsertArrayItem($_SESSION, 'main-div-center', 0);
		$this->InsertArrayItem($_SESSION, 'main-div-right', 0);
		$this->InsertArrayItem($_SESSION, 'pre_href', '');
		$this->InsertArrayItem($_SESSION, 'cur_href', '');
		$this->InsertArrayItem($_SESSION, 'login', 0);
		$this->InsertArrayItem($_SESSION, 'error_message', 0);
		$this->InsertArrayItem($_SESSION, 'group_name', '');
		$this->InsertArrayItem($_SESSION, 'office_name', '');
		$this->InsertArrayItem($_SESSION, 'purpose', '');
		$this->InsertArrayItem($_SESSION, 'place', '');
		$this->InsertArrayItem($_SESSION, 'email', '');
		$this->InsertArrayItem($_SESSION, 'phone', '');
		$this->InsertArrayItem($_SESSION, 'create_time', '');
		$this->InsertArrayItem($_SESSION, 'start_time', '');
		$this->InsertArrayItem($_SESSION, 'end_time', '');
		$this->InsertArrayItem($_SESSION, 'join_time', '');
		$this->InsertArrayItem($_SESSION, 'execute_time', '');
		$this->InsertArrayItem($_SESSION, 'full_name', '');
		$this->InsertArrayItem($_SESSION, 'user_name', '');
		$this->InsertArrayItem($_SESSION, 'level', 'member');
		$this->InsertArrayItem($_SESSION, 'password', '');
		$this->InsertArrayItem($_SESSION, 'confirm_password', '');
		$this->InsertArrayItem($_SESSION, 'id_lineup', 0);
		$this->InsertArrayItem($_SESSION, 'status', 0);
		$this->InsertArrayItem($_SESSION, 'cur_lineup_id', 0);
		$this->InsertArrayItem($_SESSION, 'last_lineup_id', 0);
		$this->InsertArrayItem($_SESSION, 'average_munite_last10', 0);
		$this->InsertArrayItem($_SESSION, 'time_gap', 0);
		$this->InsertArrayItem($_SESSION, 'search_key', '');
	}    
    function InitDB($host, $uname, $pwd, $database)
    {
        $this->db_host  = $host;
        $this->username = $uname;
        $this->pwd  = $pwd;
        $this->database  = $database;
		$this->InitSESSION();
    }
    function SetAdminEmail($email)
    {
        $this->admin_email = $email;
    }
    
    function SetWebsiteName($sitename)
    {
        $this->sitename = $sitename;
    }
    
    function GetWebsiteName()
    {
        return $this->sitename;
    }

    function SetRandomKey($key)
    {
        $this->rand_key = $key;
    }

	function PrintTableWithDataFromDatabase($table_header, $result)
	{
		$count = mysqli_num_rows($result);
		if($count == 0)
		{
			echo '<br><span class="text-danger">'.$this->GetString1('There\'re no entries found.','Không có thông tin nào được tìm thấy.').' </span><br><br>';

			return false;;
		}
		$line = 1;
		echo '<table class="table table-striped">';
			echo $table_header;
			echo '<tbody>';
				while($row = mysqli_fetch_assoc($result)) {
					echo '<tr>';
					foreach($row as $key=>$value) {
						echo '<td>',$line,'</td>';
						$line += 1;
						echo '<td>',$value,'</td>';
					}
					echo '</tr>';
				}
			echo '</tbody>';
		echo '</table>';
		echo '<br />';		
		return true;
	}
	function PrintTableWithDataFromDatabase1($table_header, $result, $href, $level)
	{
		$count = mysqli_num_rows($result);
		if($count == 0)
		{
			echo '<br><span class="text-danger">'.$this->GetString1('There\'re no entries found.','Không có thông tin nào được tìm thấy.').'</span><br><br>';

			return false;;
		}
		$line = 1;
		echo '<table class="table table-striped">';
			echo $table_header;
			echo '<tbody>';
				while($row = mysqli_fetch_assoc($result)) {
					echo '<tr>';
					foreach($row as $key=>$value) {
						if($key == 'group_name')
						{
							echo '<td>',$line,'</td>';
							$line += 1;
							echo '<td>','<a href="'.$href.'?group_name='.$value.'&level='.$level.'">'.$value.'</a>','</td>';
						}
						else
						{
							echo '<td>'.$value.'</td>';
						}
					}
					echo '</tr>';
				}
			echo '</tbody>';
		echo '</table>';
		echo '<br />';		
		return true;
	}
	function GetGroupListStatus($id)
	{
		switch($id)
		{
			case 0:
				return "Ready";
			break;
			case 1:
				return "Running";
			case 2:
				return "Closed";
			break;
			default:
				return "Invalid status";
			break;
		}
	}	
    
    function LogOut()
    {

		// remove all session variables
		session_unset(); 

		// destroy the session 
		session_destroy(); 
		
		$this->InitSESSION();		

		return true;
    }
    
    //-------Public Helper functions -------------
    function GetSelfScript()
    {
        return $_SERVER['REQUEST_URI'];
    }    
    
    function SafeDisplay($field)
    {
        return $_SESSION[$field];
    }
    
    function RedirectToPreviousURL()
    {
		$this->RedirectToURL($_SESSION['cur_href']);
	}
    function RedirectToLoginURL()
    {
		$this->RedirectToURL("login.php");
	}
    function RedirectToURL($url)
    {
        header("Location: $url");
        exit;
    }
    function IsError()
    {
		if($this->error_message == '')
		{
			return true;
		}
		
		return false;
	}
    function GetErrorMessage()
    {
		if($this->error_message != '')
		{
			echo '<div class="alert alert-danger fade in">'.'<strong>Error!</strong> '.$this->error_message.'</div>';

			$this->error_message = '';
		}
    }    
    //-------Private Helper functions-----------
    
    function HandleError($err_en, $err_vn)
    {
        $this->error_message = $this->GetString1($err_en, $err_vn);
    }

    function HandleDBError($err)
    {
        $this->db_error_message = $err;
    }
    
    function GetError()
    {
        $ret = $this->error_message;
		if($ret == '')
		{
			$ret = 'success';
		}
		$this->error_message = '';
		return $ret;
    }

    function SendUserConfirmationEmail(&$formvars)
    {
		$from = $this->GetFromAddress();
		$to   = $formvars['email'];
		$subject = "Your registration with ".$this->sitename;
		
        $confirmcode = $formvars['confirmcode'];

        $confirm_url = $this->GetAbsoluteURLFolder().'/confirmreg.php?code='.$confirmcode;

        $body ="Hello ".$formvars['name']."\r\n\r\n".
        "Thanks for your registration with ".$this->sitename."\r\n".
        "Please click the link below to confirm your registration.\r\n".
        "$confirm_url\r\n".
        "\r\n".
        "Regards,\r\n".
        "Webmaster\r\n".
        $this->sitename;

		return $this->SendEmail($from, $to, $subject, $body);		
    }
    function SendEmail($from, $to, $subject, $body)
    {
        $mailer = new PHPMailer();
        
		$mailer->IsSMTP(); // telling the class to use SMTP

        $mailer->CharSet = 'utf-8';
        
		$mailer->Host       = "ssl://smtp.gmail.com"; // SMTP server
		$mailer->SMTPDebug  = 1;                      // enables SMTP debug information (for testing)
												    // 1 = errors and messages
												    // 2 = messages only
		$mailer->SMTPAuth   = true;                   // enable SMTP authentication
		$mailer->SMTPSecure = "ssl";                  // sets the prefix to the servier
		$mailer->Host       = "smtp.gmail.com";       // sets GMAIL as the SMTP server
		$mailer->Port       = 465;                    // set the SMTP port for the GMAIL server
		$mailer->Username   = "htvdanh@gmail.com";    // GMAIL username
		$mailer->Password   = "Be1-Hu71a";            // GMAIL password

        $mailer->From = $from;        

        $mailer->AddAddress($to);

        $mailer->Subject = $subject;

        $mailer->Body = $body;

		if(!$mailer->Send()) 
		{
			echo "Mailer Error: " . $mailer->ErrorInfo;
			return false;
		} 
		else 
		{
			echo "Message sent!";
			return true;
		}		
    }

    function MySQLLogin()
    {
        $this->connection = mysqli_connect($this->db_host,$this->username,$this->pwd);

        if(!$this->connection)
        {   
            $this->HandleDBError("Database Login failed! Please make sure that the DB login credentials provided are correct");
            return false;
        }
		
		return true;
	}
    function DBLogin()
    {
        if(!$this->MySQLLogin())
        {   
            return false;
        }
        if(!mysqli_select_db($this->connection, $this->database))
        {
            $this->HandleDBError('Failed to select database: '.$this->database.' Please make sure that the database name provided is correct');
            return false;
        }
		
		$qry = "SET NAMES UTF8";

        return $this->ExeSQL($qry);
    }    
    
	/*
    Sanitize() function removes any potential threat from the
    data submitted. Prevents email injections or any other hacker attempts.
    if $remove_nl is true, newline chracters are removed from the input.
    */
    function Sanitize($str,$remove_nl=true)
    {
        $str = $this->StripSlashes($str);

        if($remove_nl)
        {
            $injections = array('/(\n+)/i',
                '/(\r+)/i',
                '/(\t+)/i',
                '/(%0A+)/i',
                '/(%0D+)/i',
                '/(%08+)/i',
                '/(%09+)/i'
                );
            $str = preg_replace($injections,'',$str);
        }

        return $str;
    }    
    function StripSlashes($str)
    {
        if(get_magic_quotes_gpc())
        {
            $str = stripslashes($str);
        }
        return $str;
    }  
	function ExeSQL1($qry, &$result)
	{
//		print "Executing $qry ..."."<br>";
		$result = mysqli_query($this->connection, $qry);
		if(!$result)
		{
		    $this->HandleDBError("Error description: " . mysqli_error($this->connection));
			return false;
		}
//		print "Done"."<br>";
		return true;
	}
	function ExeSQL($qry)
	{
//		print "Executing $qry ..."."<br>";
		if(!mysqli_query($this->connection, $qry))
		{
		    $this->HandleDBError("Error description: " . mysqli_error($this->connection));
			return false;
		}
//		print "Done"."<br>";
		return true;
	}
	function ExeSQLArray($array)
	{
		$array_count = count($array);
		
		for ($i = 0; $i < $array_count; $i++) 
		{
			if($this->ExeSQL($array[$i]) == false)
			{
				return false;
			}
		}
		
		return true;
	}

    function ProgressBar_Handler($title, $color, $percentage)
    {
		print '<label for="inputTitle">'.$title.':</label>';

		switch($color)
		{
		case 'blue':
			$class = "progress-bar progress-bar-info";
		break;
		case 'green':
			$class = "progress-bar progress-bar-success";
		break;
		case 'orange':
			$class = "progress-bar progress-bar-warning";
		break;
		case 'red':
			$class = "progress-bar progress-bar-danger";
		break;
		}
		$percentage = sprintf("%d", $percentage);
		print '<div class="progress">';
		print '	<div class="'.$class.'" style="width: '.$percentage.'%;">';
		print '		'.$percentage.'%';
		print '	</div>';
		print '</div>';
	}	
    function IsOwner($group_name)
    {
		// back-up
		$_SESSION_tmp['group_name'] = $_SESSION['group_name'];

		$this->Database_Handler('quick_groups', 'select_owner', $result);

		// restore
		$_SESSION['group_name'] = $_SESSION_tmp['group_name'];

		if($this->GetSQLResults($result, $output) == 1)
		{
			return true;
		}
		return false;
	}	
    function Quick_Control_Owner_Groups_Handler($group_name)
    {
		// back-up
		$_SESSION_tmp['group_name'] = $_SESSION['group_name'];
		
		$_SESSION['group_name'] = $group_name;
		
		$owner = $this->IsOwner($group_name);
		
		$this->Database_Handler('quick_groups', 'select', $result);
		
		$ret = true;
		if($this->GetSQLResults($result, $output) == 1)
		{
			if($owner == true)
			{
				print '<p class="text-right"><a href="edit-group.php?group_name='.$_GET['group_name'].'">'.$this->GetString1('Edit','Thay đổi').'</a></p>';
			}

			print '<label for="inputgroup_name">'.$this->GetString1('Group name:','Tên nhóm:').' <span class="text-success">'.$output['group_name'].'</span></label>'.'<br>';
			if($output['cur_lineup_id'] >= $output['last_lineup_id'])
			{
				$status = $this->GetString1('close','Đã kết thúc');
			}
			else if($output['cur_lineup_id'] == 0)
			{
				$status = $this->GetString1('not yet started','Chưa bắt đầu');
			}
			else
			{
				$status = $this->GetString1('running','Đang phục vụ');
			}

			print '<label for="inputStatus">'.$this->GetString1('Status:','Trạng thái:').' <span class="text-success">'.$status.'</span></label>'.'<br>';
			// Button Next
			if($owner == true)
			{
				if($output['cur_lineup_id'] >= $output['last_lineup_id'])
				{
					print '<button type="submit" name = "Next" class="btn btn-default pull-right disabled">'.$this->GetString1('Next','Người kế tiếp').'</button>';
				}
				else if($output['cur_lineup_id'] == 0)
				{
					print '<button type="submit" name = "Next" class="btn btn-warning pull-right">'.$this->GetString1('Next','Người kế tiếp').'</button>';
				}
				else
				{
					print '<button type="submit" name = "Next" class="btn btn-success pull-right">'.$this->GetString1('Next','Người kế tiếp').'</button>';
				}
			}

			print '<label for="inputLast_lineup_id">'.$this->GetString1('Last id in the group:','Mã số người cuối cùng trong nhóm:').'<span class="text-success">'.$output['last_lineup_id'].'</span></label>'.'<br>';
			print '<label for="inputCur_lineup_id" class="text-success">'.$this->GetString1('Currently handled id:','Mã số người đang được phục vụ:').'</label>'.'<br>';
			print '<span id="span" class="text-danger"><center>'.$output['cur_lineup_id'].'</center></span>'.'<br>';

			if($output['last_lineup_id'] == 0)
			{
				$percent = 100;
			}
			else
			{
				$percent = $output['cur_lineup_id']*100/$output['last_lineup_id'];
			}
			$this->ProgressBar_Handler($this->GetString1('Totally completed','Tỉ lệ phần trăm hoàn thành của nhóm'), 'blue', $percent);

			print '<label for="inputPlace">'.$this->GetString1('Place:','Địa điểm:').'<span class="text-success">'.$output['place'].'</span></label>'.'<br>';
			print '<label for="inputstart_time">'.$this->GetString1('Duration from:','Thời gian từ:').' <span class="text-success">'.$output['start_time'].'</span> '.$this->GetString1('To:','Tới:').' <span class="text-success">'.$output['end_time'].'</span></label>'.'<br>';

			print '<label for="inputfull_name">'.$this->GetString1('Full name:','Họ và tên:').' <span class="text-success">'.$output['full_name'].'</span></label>'.'<br>';
			print '<label for="inputoffice_name">'.$this->GetString1('Office name:','Tên cơ quan:').' <span class="text-success">'.$output['office_name'].'</span></label>'.'<br>';
			print '<label for="inputPurpose">'.$this->GetString1('Purpose:','Mục đích:').' <span class="text-success">'.$output['purpose'].'</span></label>'.'<br>';
			print '<label for="inputEmail">Email: <span class="text-success">'.$output['email'].'</span></label>'.'<br>';
			print '<label for="inputPhone">'.$this->GetString1('Phone:','Số điện thoại:').' <span class="text-success">'.$output['phone'].'</span></label>'.'<br>';

			print '<label for="inputAverage_last10">'.$this->GetString1('Average of last 10:','Thời gian trung bình của 10 lượt gần đây nhất:').' <span class="text-success">'.$output['average_munite_last10'].'</span></label>'.'<br>';
			
			goto __end;
		}
		else
		{
		    $this->HandleError('Not unique value querried.', 'Dữ liệu truy xuất không duy nhất.');
			$ret = false;
			goto __end;
		}
__end:
		// restore
		$_SESSION['group_name'] = $_SESSION_tmp['group_name'];
	}
    function Quick_Control_Select_Groups_Handler($group_name, &$output)
    {
		// back-up
		$_SESSION_tmp['group_name'] = $_SESSION['group_name'];
	
		$_SESSION['group_name'] = $group_name;
		$this->Database_Handler('quick_groups', 'select', $result);

		// restore
		$_SESSION['group_name'] = $_SESSION_tmp['group_name'];
		return $this->GetSQLResults($result, $output);
	}
    function Quick_Control_Member_Groups_Handler($group_name)
    {
		// back-up
		$_SESSION_tmp['group_name'] = $_SESSION['group_name'];

		$_SESSION['group_name'] = $group_name;
		$this->Database_Handler('quick_groups', 'select', $result);

		$ret = true;
		if($this->GetSQLResults($result, $output) != 1)
		{
		    $this->HandleError('Not unique value querried.', 'Dữ liệu truy xuất không duy nhất.');
			$ret = false;
			goto __end;
		}
		$this->Database_Handler('quick_lineups', 'select', $result2);
		if($this->GetSQLResults($result2, $output) != 1)
		{
		    $this->HandleError('Not unique value querried.', 'Dữ liệu truy xuất không duy nhất.');
			$ret = false;
			goto __end;
		}

		print '<label for="inputgroup_name">'.$this->GetString1('Group name:','Tên nhóm:').' <span class="text-success">'.$output['group_name'].'</span></label>'.'<br>';
		if($output['cur_lineup_id'] >= $output['last_lineup_id'])
		{
			$status = $this->GetString1('close','Đã kết thúc');
		}
		else if($output['cur_lineup_id'] == 0)
		{
			$status = $this->GetString1('not yet started','Chưa bắt đầu');
		}
		else
		{
			$status = $this->GetString1('running','Đang phục vụ');
		}

		print '<label for="inputStatus">'.$this->GetString1('Status:','Trạng thái:').' <span class="text-success">'.$status.'</span></label>'.'<br>';
		// Button Refresh
		print '<button type="submit" name = Refresh class="btn btn-success pull-right">'.$this->GetString1("Refresh",'Lấy thông tin mới nhất').'</button>';
		print '<label for="inputLast_lineup_id">'.$this->GetString1('Last id in the group:','Mã số cuối cùng trong nhóm:').'<span class="text-success">'.$output['last_lineup_id'].'</span></label>'.'<br>';

		print '<label for="inputMy_id"  class="text-danger">'.$this->GetString1('Currently executed in the group/My id in the group:','Mã số đang được phục vụ/Mã số của tôi').'</label>'.'<br>';

		if($output['cur_lineup_id'] > $output['id_lineup'])
		{
			print '<span id="span" class="text-default"><center>'.$output['cur_lineup_id'].'/'.$output['id_lineup'].'</center></span>'.'<br>';
		}
		else
		{
			print '<span id="span" class="text-success"><center>'.$output['cur_lineup_id'].'/'.$output['id_lineup'].'</center></span>'.'<br>';
		}

		if($output['id_lineup'] == 0)
		{
			$percent = 100;
		}
		else
		{
			$percent = $output['cur_lineup_id']*100/$output['id_lineup'];
		}
		$this->ProgressBar_Handler($this->GetString1('Totally reached','Tỉ lệ phần trăm cho tới lượt của mình'), 'blue', $percent);

		if($output['last_lineup_id'] == 0)
		{
			$percent = 100;
		}
		else
		{
			$percent = $output['cur_lineup_id']*100/$output['last_lineup_id'];
		}
		$this->ProgressBar_Handler($this->GetString1('Totally completed','Tỉ lệ phần trăm hoàn thành của nhóm'), 'red', $percent);

		print '<label for="inputPlace">'.$this->GetString1('Place:','Địa điểm:').' <span class="text-success">'.$output['place'].'</span></label>'.'<br>';
		print '<label for="inputstart_time">'.$this->GetString1('Duration from:','Thời gian từ:').' <span class="text-success">'.$output['start_time'].'</span>'.$this->GetString1('To:','Tới:').'  <span class="text-success">'.$output['end_time'].'</span></label>'.'<br>';

		print '<label for="inputfull_name">'.$this->GetString1('Full name:','Họ và Tên:').' <span class="text-success">'.$output['full_name'].'</span></label>'.'<br>';
		print '<label for="inputoffice_name">'.$this->GetString1('Office name:','Tên cơ quan:').' <span class="text-success">'.$output['office_name'].'</span></label>'.'<br>';
		print '<label for="inputPurpose">'.$this->GetString1('Purpose:','Mục đích:').' <span class="text-success">'.$output['purpose'].'</span></label>'.'<br>';
		print '<label for="inputEmail">Email: <span class="text-success">'.$output['email'].'</span></label>'.'<br>';
		print '<label for="inputPhone">'.$this->GetString1('Phone:','Số điện thoại:').' <span class="text-success">'.$output['phone'].'</span></label>'.'<br>';

		print '<label for="inputAverage_last10">'.$this->GetString1('Average of last 10:','Thời gian trung bình của 10 lượt gần đây nhất:').' <span class="text-success">'.$output['average_munite_last10'].'</span></label>'.'<br>';
		
		print '<label for="inputJoin_time">'.$this->GetString1('Join time:','Thời gian tham gia nhóm:').' <span class="text-success">'.$output['join_time'].'</span></label>'.'<br>';
		print '<label for="inputExecute_time">'.$this->GetString1('Execute time:','Thời gian được phúc vụ:').' <span class="text-success">'.$output['execute_time'].'</span></label>'.'<br>';

__end:
		// restore
		$_SESSION['group_name'] = $_SESSION_tmp['group_name'];

		return $ret;

	}
	function Quick_Search_Form_Handler()
	{
        $_SESSION['search_key'] 		= $this->Sanitize($_POST['search_key']);
        $_SESSION['start_time'] 		= $this->Sanitize($_POST['start_time']);
        $_SESSION['end_time'] 			= $this->Sanitize($_POST['end_time']);
		
		$ret = true;
		if($this->CheckFieldEmpty('start_time', 'start_time') == false)
		{
			$ret = false;
			goto __end;
		}
		
		if($this->CheckFieldEmpty('end_time', 'end_time') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CompareDateTime($_SESSION['start_time'], $_SESSION['end_time']) > 0)
		{
			$this->HandleError('Start time is bigger than end time.', 'Thời gian bắt đầu lớn hơn thời gian kết thúc.');
			$ret = false;
			goto __end;
		}
__end:		

		return $ret;
	}
    function Quick_Register_Search_Owners_Of_Groups_Handler($href)
    {
        $_SESSION['search_key'] 		= $this->Sanitize($_POST['search_key']);
        $_SESSION['start_time'] 		= $this->Sanitize($_POST['start_time']);
        $_SESSION['end_time'] 			= $this->Sanitize($_POST['end_time']);
		
		
		$ret = $this->Quick_Register_Select_Owners_Of_Groups_Handler('search_list_group', $href);
__end:		
		return $ret;
	}

    function Quick_Register_List_Owners_Of_Groups_Handler($href)
    {
		return $this->Quick_Register_Select_Owners_Of_Groups_Handler('select_list_group', $href);
	}
    function Quick_Register_Select_Owners_Of_Groups_Handler($option, $href)
    {
		$this->Database_Handler('quick_groups', $option, $result);
		if($_SESSION['vn'] == 0)
		{
			$table_header = '<thead>'.
								'<tr>'.
									'<th>Row</th>'.
									'<th>Group name</th>'.
									'<th>Full name</th>'.
									'<th>Office name</th>'.
									'<th>Place</th>'.
									'<th>Start time</th>'.
									'<th>Close time</th>'.
								'</tr>'.
							'</thead>';
		}
		else
		{
			$table_header = '<thead>'.
								'<tr>'.
									'<th>STT</th>'.
									'<th>Tên nhóm</th>'.
									'<th>Họ và tên</th>'.
									'<th>Tên cơ quan</th>'.
									'<th>Địa điểm</th>'.
									'<th>Thời gian bắt đầu</th>'.
									'<th>Thời gian kết thúc</th>'.
								'</tr>'.
							'</thead>';
		}
        return $this->PrintTableWithDataFromDatabase1($table_header, $result, $href, 'owner');

	}
    function Quick_Register_List_Members_Of_Groups_Handler($href)
    {
		$this->Database_Handler('quick_lineups', 'select_list_group', $result);
		if($_SESSION['vn'] == 0)
		{
			$table_header = '<thead>'.
								'<tr>'.
									'<th>Row</th>'.
									'<th>Group name</th>'.
									'<th>Full name</th>'.
									'<th>Office name</th>'.
									'<th>Place</th>'.
									'<th>Start time</th>'.
									'<th>Close time</th>'.
								'</tr>'.
							'</thead>';
		}
		else
		{
			$table_header = '<thead>'.
								'<tr>'.
									'<th>STT</th>'.
									'<th>Tên nhóm</th>'.
									'<th>Họ và tên</th>'.
									'<th>Tên cơ quan</th>'.
									'<th>Địa điểm</th>'.
									'<th>Thời gian bắt đầu</th>'.
									'<th>Thời gian kết thúc</th>'.
								'</tr>'.
							'</thead>';
		}
        return $this->PrintTableWithDataFromDatabase1($table_header, $result, $href, 'member');

	}
    function Quick_Login_Form_Handler()
    {
        $_SESSION['user_name'] 			= $this->Sanitize($_POST['user_name']);
        $_SESSION['password'] 			= $this->Sanitize($_POST['password']);

		if($this->CheckFieldEmpty('User name', 'user_name') == false)
		{
			return false;
		}
		if($this->CheckFieldEmpty('Password', 'password') == false)
		{
			return false;
		}
		if($this->CheckFieldExist1('quick_users') == false)
		{
			$this->HandleError('Invalid user name and password.', 'Tên đăng nhập và mật khẩu không chính xác.');
			return false;
		}

		$this->Database_Handler('quick_users', 'select', $result);

		if($this->GetSQLResults($result, $output) == 1)
		{
			$_SESSION['full_name'] = $output['full_name'];
			$_SESSION['vn'] = $output['vn'];
			$_SESSION['login'] = 1;
			return true;
		}
		
		return false;
	}
    function Quick_View_Form_Handler()
    {
		// back-up
		$_SESSION_tmp['group_name'] = $_SESSION['group_name'];

        $_SESSION['group_name'] 		= $this->Sanitize($_POST['group_name']);

		$ret = true;
		if($this->CheckFieldEmpty('Group name', 'group_name') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldExist('Group name', 'quick_groups') == false)
		{
			$ret = false;
			goto __end;
		}
__end:
		// restore
		$_SESSION['group_name'] = $_SESSION_tmp['group_name'];
		return $ret;
	}	
	function DB_Next_Form_Handler()
    {
		// back-up
		$_SESSION_tmp['group_name'] = $_SESSION['group_name'];

        $_SESSION['group_name'] 		= $this->Sanitize($_GET['group_name']);

		$_SESSION['execute_time'] = date("Y-m-d H:i:s");

		$ret = $this->DB_Next_Handler();

		// restore
		$_SESSION['group_name'] = $_SESSION_tmp['group_name'];

		return $ret;
	}
    function DB_Next_Handler()
    {
		// back-up
		$_SESSION_tmp['cur_lineup_id'] = $_SESSION['cur_lineup_id'];

		$ret = true;
		// Transaction
        if(!$this->DBLogin())
        {
            $this->HandleError('Database login failed.', 'Đăng nhập CSDL bị lỗi.');
			$ret = false;
			goto __end;
		}     
		$qry = "START TRANSACTION";
		if($this->ExeSQL1($qry, $result) == false)
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}

		
		$qry = $this->GetSQL('quick_groups', 'update_cur_lineup_id_plus1');
		if($this->ExeSQL1($qry, $result) == false)
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}

		$qry = $this->GetSQL('quick_groups', 'select_owner');
		if($this->ExeSQL1($qry, $result) == false)
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}

		if($this->GetSQLResults($result, $output) == 1)
		{

			$_SESSION['cur_lineup_id'] = $output['cur_lineup_id'];
		}
		else
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}

		$qry = $this->GetSQL('quick_lineups', 'update_execute');
		if($this->ExeSQL1($qry, $result) == false)
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}
		$ret = $this->ExeSQL1("COMMIT", $result);
__end:
		// restore
		$_SESSION['cur_lineup_id'] = $_SESSION_tmp['cur_lineup_id'];

		return $ret;
	}	

    function DB_Join_Handler()
    {
		// back-up
		$_SESSION_tmp['id_lineup'] = $_SESSION['id_lineup'];
		$_SESSION_tmp['last_lineup_id'] = $_SESSION['last_lineup_id'];

		$ret = true;
		// Transaction
        if(!$this->DBLogin())
        {
            $this->HandleError('Database login failed.', 'Đăng nhập CSDL bị lỗi.');
			$ret = false;
			goto __end;
        }     
		$qry = "START TRANSACTION";
		if($this->ExeSQL1($qry, $result) == false)
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}

		$qry = $this->GetSQL('quick_lineups', 'select_max_id_lineup');
		if($this->ExeSQL1($qry, $result) == false)
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}

		if($this->GetSQLResults($result, $output) == 1)
		{
			$_SESSION['id_lineup'] = $output['max'] + 1;
		}
		else
		{
			$_SESSION['id_lineup'] = 1;
		}
		
		$qry = $this->GetSQL('quick_lineups', 'insert');
		
		$ret = $this->ExeSQL1($qry, $result);
		
		if($ret == false)
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}

		$_SESSION['last_lineup_id'] = $_SESSION['id_lineup'];
		
		$qry = $this->GetSQL('quick_groups', 'update_last_lineup_id');
		$ret = $this->ExeSQL1($qry, $result);

		if($ret == false)
		{
			$this->ExeSQL1("ROLLBACK", $result);
			$ret = false;
			goto __end;
		}

		$ret = $this->ExeSQL1("COMMIT", $result);
__end:
		// restore
		$_SESSION['last_lineup_id'] = $_SESSION_tmp['last_lineup_id'];
		$_SESSION['id_lineup'] = $_SESSION_tmp['id_lineup'];
		
		return $ret;
	}	
    function Quick_Join_Form_Handler()
    {
		// back-up
		$_SESSION_tmp['group_name'] = $_SESSION['group_name'];

        $_SESSION['group_name'] 		= $this->Sanitize($_POST['group_name']);

		$ret = true;
		if($this->CheckFieldEmpty('Group name', 'group_name') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldExist('Group name', 'quick_groups') == false)
		{
			$ret = false;
			goto __end;
		}
		$_SESSION['join_time'] = date("Y-m-d H:i:s");
		
		$this->Database_Handler('quick_groups', 'select_owner', $result);
		
		$ret = true;
		if($this->GetSQLResults($result, $output) == 1)
		{
            $this->HandleError('The owner of the group is not allowed to join.', 'Người thành lập nhóm không được phép tham gia nhóm như là thành viên.');
			$ret = false;
			goto __end;
		}

		$this->Database_Handler('quick_lineups', 'select', $result);

		if($this->GetSQLResults($result, $output) == 1)
		{
            $this->HandleError('Already joined.', 'Đã tham gia.');
			$ret = false;
			goto __end;
		}
		$ret = $this->DB_Join_Handler();
		
__end:
		// restore
		$_SESSION['group_name'] = $_SESSION_tmp['group_name'];
		return $ret;
	}
	
    function Quick_Register_New_Group_Form_Handler()
    {
		// back-up
		$_SESSION_tmp['group_name'] 	= $_SESSION['group_name'];
		$_SESSION_tmp['office_name'] 	= $_SESSION['office_name'];
		$_SESSION_tmp['purpose'] 		= $_SESSION['purpose'];
		$_SESSION_tmp['place'] 			= $_SESSION['place'];
		$_SESSION_tmp['phone'] 			= $_SESSION['phone'];
		$_SESSION_tmp['email']			= $_SESSION['email'];
		$_SESSION_tmp['start_time'] 	= $_SESSION['start_time'];
		$_SESSION_tmp['end_time'] 		= $_SESSION['end_time'];

		
        $_SESSION['group_name'] 		= $this->Sanitize($_POST['group_name']);
        $_SESSION['office_name'] 		= $this->Sanitize($_POST['office_name']);
        $_SESSION['purpose'] 			= $this->Sanitize($_POST['purpose']);
        $_SESSION['place'] 				= $this->Sanitize($_POST['place']);
        $_SESSION['phone'] 				= $this->Sanitize($_POST['phone']);
        $_SESSION['email'] 				= $this->Sanitize($_POST['email']);
        $_SESSION['start_time'] 		= $this->Sanitize($_POST['start_time']);

        $_SESSION['end_time'] 			= $this->Sanitize($_POST['end_time']);

		$ret = true;
		if($this->CheckFieldEmptyAndExist('Group name', 'quick_groups', 'group_name') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Office name', 'office_name') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Purpose', 'purpose') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Place', 'place') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Phone', 'phone') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldInt('Phone', 'phone') == false)
		{
			$ret = false;
			goto __end;
		}
		
		if($this->CheckFieldEmpty('Email', 'email') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmailValid('Email', 'email') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Start time', 'start_time') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldValidDateTime('Start time', 'start_time') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Close time', 'end_time') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldValidDateTime('Close time', 'end_time') == false)
		{
			$ret = false;
			goto __end;
		}

		if (!isset($_SESSION['captcha_code']) 
		|| ($_SESSION['captcha_code'] != trim($_POST['captcha'])))
		{
			$this->HandleError('Captcha incorrect.', 'Captcha không đúng.');
			$ret = false;
			goto __end;
		}

		$_SESSION['create_time'] = date("Y-m-d H:i:s");
		
		$ret = $this->Database_Handler('quick_groups', 'insert', $result);
__end:
		// restore
		$_SESSION['group_name'] 	= $_SESSION_tmp['group_name'];
		$_SESSION['office_name'] 	= $_SESSION_tmp['office_name'];
		$_SESSION['purpose'] 		= $_SESSION_tmp['purpose'];
		$_SESSION['place'] 			= $_SESSION_tmp['place'];
		$_SESSION['phone'] 			= $_SESSION_tmp['phone'];
		$_SESSION['email']			= $_SESSION_tmp['email'];
		$_SESSION['start_time'] 	= $_SESSION_tmp['start_time'];
		$_SESSION['end_time'] 		= $_SESSION_tmp['end_time'];
		return $ret;
	}
	
    function Quick_Register_Edit_Group_Form_Handler()
    {
		// back-up
		$_SESSION_tmp['group_name'] 	= $_SESSION['group_name'];
		$_SESSION_tmp['office_name'] 	= $_SESSION['office_name'];
		$_SESSION_tmp['purpose'] 		= $_SESSION['purpose'];
		$_SESSION_tmp['place'] 			= $_SESSION['place'];
		$_SESSION_tmp['phone'] 			= $_SESSION['phone'];
		$_SESSION_tmp['email']			= $_SESSION['email'];
		$_SESSION_tmp['start_time'] 	= $_SESSION['start_time'];
		$_SESSION_tmp['end_time'] 		= $_SESSION['end_time'];


        $_SESSION['group_name'] 		= $this->Sanitize($_POST['group_name']);
        $_SESSION['office_name'] 		= $this->Sanitize($_POST['office_name']);
        $_SESSION['purpose'] 			= $this->Sanitize($_POST['purpose']);
        $_SESSION['place'] 				= $this->Sanitize($_POST['place']);
        $_SESSION['phone'] 				= $this->Sanitize($_POST['phone']);
        $_SESSION['email'] 				= $this->Sanitize($_POST['email']);
        $_SESSION['start_time'] 		= $this->Sanitize($_POST['start_time']);

        $_SESSION['end_time'] 			= $this->Sanitize($_POST['end_time']);

		$ret = true;
		if($this->CheckFieldEmpty('Office name', 'office_name') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Purpose', 'purpose') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Place', 'place') == false)
		{
			return false;
		}
		if($this->CheckFieldEmpty('Phone', 'phone') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldInt('Phone', 'phone') == false)
		{
			$ret = false;
			goto __end;
		}
		
		if($this->CheckFieldEmpty('Email', 'email') == false)
		{
			return false;
		}
		if($this->CheckFieldEmailValid('Email', 'email') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Start time', 'start_time') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldValidDateTime('Start time', 'start_time') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Close time', 'end_time') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldValidDateTime('Close time', 'end_time') == false)
		{
			$ret = false;
			goto __end;
		}

		if (!isset($_SESSION['captcha_code']) 
		|| ($_SESSION['captcha_code'] != trim($_POST['captcha'])))
		{
			$this->HandleError('Captcha incorrect.', 'Captcha không đúng.');
			$ret = false;
			goto __end;
		}

		$ret = $this->Database_Handler('quick_groups', 'update', $result);
__end:
		// restore
		$_SESSION['group_name'] 	= $_SESSION_tmp['group_name'];
		$_SESSION['office_name'] 	= $_SESSION_tmp['office_name'];
		$_SESSION['purpose'] 		= $_SESSION_tmp['purpose'];
		$_SESSION['place'] 			= $_SESSION_tmp['place'];
		$_SESSION['phone'] 			= $_SESSION_tmp['phone'];
		$_SESSION['email']			= $_SESSION_tmp['email'];
		$_SESSION['start_time'] 	= $_SESSION_tmp['start_time'];
		$_SESSION['end_time'] 		= $_SESSION_tmp['end_time'];
		return $ret;
	}	
    function Quick_Sign_Up_Form_Handler()
    {
        $_SESSION['full_name'] 			= $this->Sanitize($_POST['full_name']);
        $_SESSION['user_name'] 			= $this->Sanitize($_POST['user_name']);
		$_SESSION['level'] 				= 'owner';
        $_SESSION['password'] 			= $this->Sanitize($_POST['password']);
        $_SESSION['confirm_password'] 	= $this->Sanitize($_POST['confirm_password']);
		$_SESSION['create_time'] 		= date("Y-m-d H:i:s");
		$_SESSION['vn'] 				= $this->Sanitize($_POST['vn']);
		
		$ret = true;
		if($this->CheckFieldEmpty('Full name', 'full_name') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('User name', 'user_name') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Password', 'password') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldEmpty('Confirm Password', 'confirm_password') == false)
		{
			$ret = false;
			goto __end;
		}
		if($this->CheckFieldMatch('Password', 'password', 'confirm_password') == false)
		{
			$ret = false;
			goto __end;
		}

		if (!isset($_SESSION['captcha_code']) 
		|| ($_SESSION['captcha_code'] != trim($_POST['captcha'])))
		{
			$this->HandleError('Captcha incorrect.', 'Captcha không đúng.');
			$ret = false;
			goto __end;
		}

		if($this->CheckFieldExist1('quick_users') == false)
		{
			if($this->Database_Handler('quick_users', 'insert', $result))
			{
				$_SESSION['login'] = 1;
				goto __end;
			}
			else
			{
				$this->HandleError('User name already exists.', 'Tên đăng nhập đã tồn tại.');
				$ret = false;
				goto __end;
			}
		}
		else
		{
			$this->HandleError('User name already exists.', 'Tên đăng nhập đã tồn tại.');
			$ret = false;
			goto __end;
		}
__end:
		return $ret;
	}
	
	function Database_Handler($table, $option, &$result)
	{
        if(!$this->DBLogin())
        {
            $this->HandleError('Database login failed.', 'Đăng nhập CSDL bị lỗi.');
            return false;
        }          
		$qry = $this->GetSQL($table, $option);
		
		return $this->ExeSQL1($qry, $result);
	}	

    function CheckFieldMatch($fieldname, $field1, $field2)
    {            
		if($_SESSION[$field1] != $_SESSION[$field2])
		{
			$this->HandleError('"'.$fieldname.'" does not match.', '"'.$fieldname.'" không giống nhau.');
			return false;
		}
		else
		{
			return true;
		}
	}
    function CheckFieldEmailValid($fieldname, $field)
    {            
		if (filter_var($_SESSION[$field], FILTER_VALIDATE_EMAIL) == true) 
		{
			return true;
		}
		else
		{
			$this->HandleError('"'.$fieldname.'" is invalid format.', '"'.$fieldname.'" bị sai định dạng.');
			return false;
		}
	}
    function CheckFieldInt($fieldname, $field)
    {            
		if (ctype_digit($_SESSION[$field])) 
		{
			return true;
		}
		else
		{
			$this->HandleError('"'.$fieldname.'" must be all digits.', '"'.$fieldname.'" tất cả phải là ký tự số.');
			return false;
		}
	}
	function CheckFieldValidDateTime($fieldname, $field)
	{ 
	   if (DateTime::createFromFormat('Y-m-d H:i:s', $_SESSION[$field]) == FALSE) 
	   { 
			$this->HandleError('"'.$fieldname.'" is invalid datetime format.', '"'.$fieldname.'" bị sai định dạng thời gian.');
			return false; 
		} 
		else
		{
			return true; 
		}
	} 
    function CompareDateTime($val1, $val2)
    {        
		$datetime1 = new DateTime($val1);
		$datetime2 = new DateTime($val2);

		if($datetime1 > $datetime2)
		{
			return 1;
		}
		else if($datetime1 < $datetime2)
		{
			return -1;
		}
		
		return 0;
	}
    function CheckFieldEmpty($fieldname, $field)
    {            
		if($_SESSION[$field] == '')
		{
			$this->HandleError('"'.$fieldname.'" is empty.', '"'.$fieldname.'" bị rỗng.');
			return false;
		}
		else
		{
			return true;
		}
	}
    function CheckFieldNotExist($fieldname, $table)
    {
		$qry = $this->GetSQL($table, 'select');
		
		if($this->GetUniqueValueFromDatabase($qry, $output) >= 1)
		{
			$this->HandleError('"'.$fieldname.'" already exists.', '"'.$fieldname.'" đã tồn tại.');
			return false;
		}
		else
		{
			return true;
		}
	}
    function CheckFieldExist($fieldname, $table)
    {
		$qry = $this->GetSQL($table, 'select');
		
		if($this->GetUniqueValueFromDatabase($qry, $output) >= 1)
		{
			return true;
		}
		else
		{
			$this->HandleError('"'.$fieldname.'" does not exist.', '"'.$fieldname.'" không tồn tại.');
			return false;
		}
	}
    function CheckFieldExist1($table)
    {
		$qry = $this->GetSQL($table, 'select');
		
		if($this->GetUniqueValueFromDatabase($qry, $output) >= 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
    function CheckFieldEmptyAndExist($fieldname, $table, $field)
	{
		if($this->CheckFieldEmpty($fieldname, $field) == false)
		{
			return false;
		}
		
		return $this->CheckFieldNotExist($fieldname, $table);
	}
    function GetSQLResults($result, &$output)
    {
        $count = mysqli_num_rows($result);
		
		while($row = mysqli_fetch_assoc($result)) 
		{
			foreach($row as $key=>$value) 
			{
				$output[$key] = $value;
			}
		}
        return $count;
	}
    function InsertArrayItem(&$array, $key, $value)
    {
		if(array_key_exists($key, $array) == false) 
		{
			$array[$key] = $value;
		}
	}
	
    function GetUniqueValueFromDatabase($qry, &$output)
    {
        if(!$this->DBLogin())
        {
            $this->HandleError('Database login failed.', 'Đăng nhập CSDL bị lỗi.');
            return -1;
        }          
		$this->ExeSQL1($qry, $result);
        
        if(!$result)
        {
            $this->HandleError('Invalid unique data.', 'Dữ liệu truy xuất không duy nhất.');
            return -1;
        }
        
        $count = mysqli_num_rows($result);
		
		if($count == 1)
		{
			$row = mysqli_fetch_assoc($result);
			foreach($row as $key=>$value) 
			{
				$output = $value;
				break;
			}
		}
        return $count;
    }

	function ToggleLanguage()
	{
		$_SESSION_tmp['vn'] = $_SESSION['vn'];
		$_SESSION['vn'] = ($_SESSION['vn'] == 1) ? 0 : 1;
		if($this->Database_Handler('quick_users', 'update_language', $result) == false)
		{
			$_SESSION['vn'] = $_SESSION_tmp['vn'];
			$this->HandleError('Toggling language failed.', 'Chuyển ngôn ngữ thất bại.');
		}
		return true;
	}
	function GetString($en, $vn)
	{
		if($_SESSION['vn'] == 0) 
		{
			print $en; 
			return $en;
		}
		else 
		{
			print $vn;
			return $vn;
		}	
	}
	function GetString1($en, $vn)
	{
		if($_SESSION['vn'] == 0) 
		{
			return $en;
		}
		else 
		{
			return $vn;
		}	
	}
	function GetSQL($table, $option)
	{
		$qry['quick_users']['select'] = 'SELECT full_name, vn FROM quick_users WHERE (user_name = "'.$_SESSION['user_name'].'") AND (password = md5("'.$_SESSION['password'].'"))';
		$qry['quick_users']['insert'] = 'INSERT INTO quick_users(full_name, user_name, password, level, create_time, vn) VALUES ("'.$_SESSION["full_name"].'","'.$_SESSION["user_name"].'",md5("'.$_SESSION["password"].'"),"'.$_SESSION["level"].'","'.$_SESSION["create_time"].'",'.$_SESSION["vn"].')';
		$qry['quick_users']['update_language'] = 'UPDATE quick_users SET vn='.$_SESSION["vn"].' WHERE (user_name = "'.$_SESSION['user_name'].'")';
		$qry['quick_groups']['select'] = 'SELECT * FROM quick_groups WHERE (group_name = "'.$_SESSION["group_name"].'")';
		$qry['quick_groups']['select_list_group'] = 'SELECT group_name, full_name, office_name, place, start_time, end_time FROM quick_groups WHERE (user_name = "'.$_SESSION["user_name"].'")';
		$qry['quick_groups']['search_list_group'] = 'SELECT group_name, full_name, office_name, place, start_time, end_time FROM quick_groups WHERE ((group_name LIKE "%'.$_SESSION["search_key"].'%") OR (full_name LIKE "%'.$_SESSION["search_key"].'%") OR (office_name LIKE "%'.$_SESSION["search_key"].'%") OR (place LIKE "%'.$_SESSION["search_key"].'%")) AND (start_time >= "'.$_SESSION["start_time"].'") AND (start_time <= "'.$_SESSION["end_time"].'")';
		$qry['quick_groups']['select_owner'] = 'SELECT * FROM quick_groups WHERE (user_name = "'.$_SESSION["user_name"].'") AND (group_name = "'.$_SESSION["group_name"].'")';
		$qry['quick_groups']['insert'] = 'INSERT INTO quick_groups(full_name, user_name, office_name, group_name, purpose, place, email, phone, start_time, end_time, create_time, status, cur_lineup_id, last_lineup_id, average_munite_last10) VALUES ("'.$_SESSION["full_name"].'", "'.$_SESSION["user_name"].'", "'.$_SESSION["office_name"].'", "'.$_SESSION["group_name"].'", "'.$_SESSION["purpose"].'", "'.$_SESSION["place"].'", "'.$_SESSION["email"].'", "'.$_SESSION["phone"].'", "'.$_SESSION["start_time"].'", "'.$_SESSION["end_time"].'", "'.$_SESSION["create_time"].'", '.$_SESSION["status"].', '.$_SESSION["cur_lineup_id"].', '.$_SESSION["last_lineup_id"].', '.$_SESSION["average_munite_last10"].')';

		$qry['quick_groups']['update'] = 'UPDATE quick_groups SET purpose="'.$_SESSION["purpose"].'",place="'.$_SESSION["place"].'",email="'.$_SESSION["email"].'",phone="'.$_SESSION["phone"].'",start_time="'.$_SESSION["start_time"].'",end_time="'.$_SESSION["end_time"].'",status="'.$_SESSION["status"].'",office_name="'.$_SESSION["office_name"].'" WHERE (group_name = "'.$_SESSION["group_name"].'")';

		$qry['quick_groups']['update_cur_lineup_id_plus1'] = 'UPDATE quick_groups SET cur_lineup_id = (SELECT cur_lineup_id + 1) WHERE (cur_lineup_id < last_lineup_id) AND (group_name = "'.$_SESSION["group_name"].'")';
		$qry['quick_groups']['update_last_lineup_id'] = 'UPDATE quick_groups SET last_lineup_id = '.$_SESSION["last_lineup_id"].' WHERE (group_name = "'.$_SESSION["group_name"].'")';
		$qry['quick_lineups']['select'] = 'SELECT * FROM quick_lineups WHERE (group_name = "'.$_SESSION["group_name"].'" ) AND (user_name = "'.$_SESSION["user_name"].'")';
		$qry['quick_lineups']['select_list_group'] = 'SELECT group_name, full_name, office_name, place, start_time, end_time FROM quick_groups WHERE (group_name = (SELECT DISTINCT group_name FROM quick_lineups WHERE (user_name = "'.$_SESSION["user_name"].'")))';
		$qry['quick_lineups']['select_max_id_lineup'] = 'SELECT MAX(id_lineup) AS max FROM quick_lineups WHERE (group_name = "'.$_SESSION["group_name"].'")';
		$qry['quick_lineups']['insert'] = 'INSERT INTO quick_lineups(id_lineup, group_name, user_name, join_time, execute_time, time_gap) VALUES ('.$_SESSION["id_lineup"].', "'.$_SESSION["group_name"].'", "'.$_SESSION["user_name"].'", "'.$_SESSION["join_time"].'", "'.$_SESSION["execute_time"].'", '.$_SESSION["time_gap"].')';
		$qry['quick_lineups']['update_execute'] = 'UPDATE quick_lineups SET execute_time = "'.$_SESSION["execute_time"].'" WHERE (id_lineup = '.$_SESSION["cur_lineup_id"].') AND (group_name = "'.$_SESSION["group_name"].'")';
		
		return $qry[$table][$option];
	}	
}	
?>